<?php

namespace App\Http\Controllers\Admin;
use \Hash;
use \DB;
use Illuminate\Http\Request;
use App\Http\Requests\UserInsertRequest;
use App\Http\Requests\UserUpdateRequest;
use App\Http\Requests;
use App\Http\Controllers\Controller;

class UserController extends Controller
{

	//获取主页
	    public function getIndex(Request $request)
	    {
	    	//获取查询条件的数据
	    	$num = $request->input('num',3);
	    	//连接数据库获取所有数据
	    	// $users = DB::table('shop_users')->get();
	    	//获取所有的查询条件
	    	$lists = $request->all();
	    	// dd($lists);
	    	if(!empty($lists['page']))
	    		{ $page = $lists['page'];}else
	    	{
	    			$page =1;
	    	 }
	    	//dd($lists);
	    	// dd($lists);
	    	//判断有没有 查询条件
	    	if($request->input('keyword'))
	    		{
	    			$users = DB::table('shop_users')->where('uname','like','%'.$request->input('keyword').'%')->paginate($num);
	    			
	    		}else{
	    			
	    			$users = DB::table('shop_users')->paginate($num);
	    		}

	    	//定义一个数组
	    		$sex=['m'=>'男','w'=>'女','x'=>'未知'];
	    	return view('admin.user.index',['users'=>$users,'lists'=>$lists,'sex'=>$sex,'page'=>$page]);
	    	
	    }

	//用户添加操作
	    public function getAdd()
	    {
	    	return view('admin.user.add');
	    	
	    }

	//处理用户添加提交过来的数据
	    public function postDoadd(UserInsertRequest $request)
	    {
			
			$data = $request->only(['uname','email','tel','sex','upwd']);
			$data['upwd']= Hash::make($data['upwd']);
			$data['token']=str_random(50);
			$data['pic']=$this->upload($request);
			if($data['pic']=='false'){
				return back()->with('error','图片上传失败');
			}
			$data['regtime']=time();
			//链接数据库进行操作
			$res = DB::table('shop_users')->insert($data);
			
			if($res)
			{
				return redirect('./admin/user/index')->with('success','插入数据库成功');
			}else{
				return back()->with('error','用户添加失败');
			}
			    	
	    }

	    //处理图片的函数
	    private function upload($request)
	    {
	    	// dd($request->hasFile('pic'));
	    	if($request->hasFile('pic'))
	    	{
	    		//获取图片的后缀名
	    		$exname = $request->file('pic')->getClientOriginalExtension();
	    		//拼接新的文件名
	    		$newname = md5(rand(1111,9999).time());
	    		//进行移动操作
	    		$request->file('pic')->move('./upload/',$newname.'.'.$exname);
	    		return '/upload/'.$newname.'.'.$exname;
	    	}else{
	    		// dd(22222);
	    		return 'false';
	    		// return back();
	    	}
	    }

	    //删除的函数
	    public function postDelete(Request $request)
	    {
	    	//获取id
	    	$uid = $request->input('uid');
	    	// $id = $uid->uid;
	    	// return $uid;
	    	// die;
	    	$res = DB::table('shop_users')->where('uid',$uid)->delete();
	    	return $res;
	    }

	    //修改的函数

	    public function getEdit(Request $request)
	    {

	    	$uid = $request->all()['id'];
	    	$page = $request->all()['page'];


	    	//连接数据库进行查找
	    	$user = DB::table('shop_users')->where('uid',$uid)->first();
	    	// dd($user); 
	    	//解析模板

	    	return view('admin.user.edit',['user'=>$user,'page'=>$page]);


	    }

	    //处理修改的数据
	    public function postDoedit(UserUpdateRequest $request)
	    {
	    		
	    		//处理提交过来的数据

	    		$data = $request->only(['uname','sex','email','tel']);
	    		$page = $request->all()['page'];
	    		//获取id
	    		$uid = $request->all()['uid'];
	    		// echo $page;die;
	    		//判断是否传图片
	    		if($request->hasFile('pic'))
	    		{
	    			 
	    			 //拿到原有图片的拓展名
	    			$ex = $request->file('pic')->getClientOriginalExtension();
	    			//构造新文件名
	    			$newname = md5(time().rand(1,9999));
	    			//进行移动
	    			$request->file('pic')->move('./upload/',$newname.'.'.$ex);
	    			$data['pic']= '/upload/'.$newname.'.'.$ex;
	    			// var_dump($data);


	    		}else{
	    			echo '没有传图片';
	    		}
	    	//执行修改
	    		$res=DB::table('shop_users')->where('uid',$uid)->update($data);
	    		if($res)
	    		{
	    			return redirect('/admin/user/index?page='.$page)->with('success','修改成功');
	    		}else{
	    			return back()->with('error','修改失败');

	    		}

	    }

}
